package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.AddAddressBeen;
import cn.xfdzx.android.apps.shop.bean.SelectImageBeen;
import cn.xfdzx.android.apps.shop.contract.AddAddressContract;
import cn.xfdzx.android.apps.shop.contract.SelectImageContract;
import cn.xfdzx.android.apps.shop.model.AddAddressModel;
import cn.xfdzx.android.apps.shop.model.SelectImageModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class SelectImagePresenter extends BasePresenter<SelectImageContract.View> implements SelectImageContract.Presenter {

    private SelectImageContract.Model model;

    public SelectImagePresenter() {
        model = new SelectImageModel();
    }


    @Override
    public void selectImage(String realname) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.selectImage(realname)
                .compose(RxScheduler.<SelectImageBeen>Flo_io_main())
                .as(mView.<SelectImageBeen>bindAutoDispose())
                .subscribe(new Consumer<SelectImageBeen>() {
                    @Override
                    public void accept(SelectImageBeen bean) throws Exception {
                        mView.onSelectImage(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }
}
